﻿/* 
 *  <copyright file="PlatformerScreen.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using Microsoft.Xna.Framework;
using SEGameExample.InputMappers;
using SobrietyEngine.Actor;
using SobrietyEngine.Assets;
using SobrietyEngine.Assets.Textures;
using SobrietyEngine.Events;
using SobrietyEngine.Input.Mapping;
using SobrietyEngine.Logging;
using SobrietyEngine.Physics;
using SobrietyEngine.PlayerData;
using SobrietyEngine.Scene;
using SobrietyEngine.Screens;
using SobrietyEngine.User;

namespace SEGameExample.Screens
{
    class BaseLevel: BaseScreen, IEventListener
    {
        protected PlayerGameService playerGameService;
        protected IUserGameService userGameService;
        protected SceneGameService SceneGameService;
        protected TextureManagerGameService textureManager;
        protected FarseerPhysicsService physicsGameService;
        protected ActorGameService actorGameService;
        protected IAssetGameService assetGameService;

        protected String SceneName;
        protected User PlayerOne;
        protected IScene Scene;

        public BaseLevel(String sceneName)
        {
            SceneName = sceneName;
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            SceneGameService.ManualDraw(gameTime);
        }

        public override void Initialize()
        {
            base.Initialize();

            physicsGameService = (FarseerPhysicsService)ScreenManager.Game.Services.GetService(typeof(IPhysicsGameService));
            //set our physics options
            physicsGameService.Enabled = true;
            physicsGameService.Visible = true;
            physicsGameService.CreateDebugViews = true;

            SceneGameService = (SceneGameService)ScreenManager.Game.Services.GetService(typeof(ISceneGameService));            
            SceneGameService.AddScene(SceneName, new FlatList3DFarseerScene(SceneName, ScreenManager.Game, new Vector2(0,-9.8f) ));
            SceneGameService.Scenes[SceneName].Debug = true;
            SceneGameService.Scenes[SceneName].ActorPositionLimit = new Vector3(1000, 1000, 1000);
            Scene = SceneGameService.Scenes[SceneName];

            playerGameService = (PlayerGameService)ScreenManager.Game.Services.GetService(typeof(PlayerGameService));
            textureManager = (TextureManagerGameService)ScreenManager.Game.Services.GetService(typeof(ITextureManagerGameService));
            actorGameService = (ActorGameService)ScreenManager.Game.Services.GetService(typeof(IActorGameService));
            userGameService = (IUserGameService)ScreenManager.Game.Services.GetService(typeof(IUserGameService));
            assetGameService = (IAssetGameService)ScreenManager.Game.Services.GetService(typeof(IAssetGameService));

            //turn on logging of some events
            EventRouter.LogRaisedEvents = false;

            //set some logging options
            Logger.SetLoggingType(Logger.LogType.None);

            //connect to the first PlayerOne we find
            foreach (User activeUser in userGameService.Users)
            {
                if (activeUser.Active)
                {
                    if (PlayerOne == null)
                    {
                        PlayerOne = activeUser;
                        break;
                    }
                }
            }            
            
            SceneGameService.Enabled = true;           
        }

        public override void RegisterEventListeners()
        {
            EventRouter.AddEventListener(typeof(SEGameExample.Events.WorldMap.ExitLevel), this);
            ListenersActive = true;
        }

        public override void UnregisterEventListeners()
        {
            EventRouter.RemoveEventListener(this);
            ListenersActive = false;
        }

        void IEventListener.HandleEvent(Event e)
        {
            if (e.Type == typeof(SEGameExample.Events.WorldMap.ExitLevel))
            {
                //out we go - clear our Scene and physics world, remove our input mappers, and get rid of this screen
                SceneGameService.Scenes[SceneName].Clear();
                SceneGameService.Scenes.Remove(SceneName);

                PlayerOne.RemoveInputMapper(typeof(PlatformerEventMapper));
                ScreenManager.RemoveScreen(this);

                //we have the event listener for 'exit level' on our world map screen.
                //it will handle the event, start worldUp the Scene, and reattach our world controllers for us
            }
        }

        public override void Update(GameTime gameTime, Boolean coveredByOtherScreen, IScreen otherScreen)
        {
            base.Update(gameTime, coveredByOtherScreen, otherScreen);
        }
    }
}